<?php
/**
 * 全局短信模块,发送验证码及验证。
 */
class SmsController extends Controller
{
    /**
     * 发送短信验证码
     * @return [type] [description]
     */
    public function actionSend()
    {
        //type 验证码类型  1:注册  2:找回密码
        $result=array();
        $sms_param =array();
        $sms_template_code='';
        $extend='';
        $extra = array();
        $result['error']=0;
        $result['error_msg']='发送成功';
        // if (false) {
        //     $result['error_msg'] = '请使用PC端的短信验证码发送接口';
        //     $result['error'] = 1;
        //     echo json_encode($result);
        //     return;
        // }
        $mobile = Yii::app()->request->getPost('mobile');

        $type = Yii::app()->request->getPost('type');
        $explain = Yii::app()->request->getPost('explain', '您的验证码为');
        if (empty($mobile) ) {
            $result['error_msg'] = '手机号码错误';
            $result['error'] = 1;
            echo json_encode($result);
            return;
        }
        if (empty($type) || !in_array($type, array(1,2)) ) {
            $result['error_msg'] = '类型不存在';
            $result['error'] = 2;
            echo json_encode($result);
            return;
        }
        $is_old = User::model()->checkMobile($mobile);
        if ($type == 1) {
            if (!empty($is_old)) {
                $result['error_msg'] = '手机号码已存在,请登录';
                $result['error'] = 3;
                echo json_encode($result);
                return;
            }
            $sms_template_code='SMS_86540065';
        }
        if ($type == 2) { 
            if (empty($is_old)) {
                $result['error_msg'] = '手机号码不存在,请先注册';
                $result['error'] = 4;
                echo json_encode($result);
                return;
            }
            $sms_template_code='SMS_86650054';
        }
        
        $captcha = mt_rand(1000, 9999);
        $sms_param['code'] = ''.$captcha.'';
        $code = array();
        $code['code'] = $captcha;
        Yii::app()->cache->set('captcha_' . $mobile, $captcha, 10 * 60);
        $extend = '';
        $content = $explain. $captcha."(10分钟内有效): ";
        $return_data = array();
        if (!YII_DEBUG || true) {  
            //$return_data = SMS::send($content, $mobile);
            
            $mobile = User::model()->getNociceMobileByMobile($mobile);

            $return_data = WxUtil::aliyunSend($mobile,$sms_template_code,$code,$extra);
        } else {
            $result['content'] = $content;
        }
        //记录日志
        $log = array();
        $log['sms_param'] = $sms_param;
        $log['code'] = $code;
        $log['rec_num'] = $mobile;
        $log['sms_template_code'] = $sms_template_code;
        $log['extend'] = $extend;
        $log['result'] = $return_data;
        $log['is_web'] = 1;
        file_put_contents(Yii::app()->getRuntimePath() . '/send_sms_' . date("Y_W") . '.txt', sprintf("%s %s %s", date("Y-m-d H:i:s"), json_encode($log), PHP_EOL), FILE_APPEND);
        echo json_encode($result);
    }

    /**
     * 校验 验证码
     * @return [type] [description]
     */
    public function actionValidate()
    {
        $result = array('error' => 0);
        $mobile = Yii::app()->request->getQuery('mobile');
        $code = Yii::app()->request->getQuery('code');
        $sys_code = Yii::app()->cache->get('captcha_' . $mobile);
        if (!empty($sys_code) && $sys_code == $code) {
            Yii::app()->cache->delete('captcha_' . $mobile);
            echo 'true';
        } else {
            echo 'false';
        }
    }
}